Getting started with SGPLOT

您所在的位置:网站首页 sas groupdisplay=cluster Getting started with SGPLOT

Getting started with SGPLOT

2023-08-28 09:55| 来源: 网络整理| 查看: 265

This is the 2nd installment of the "Getting Started" series, and the audience is the user who is new to the SG Procedures. It is quite possible that an experienced users may also find some useful nuggets here.

One of the most popular and useful graph types is the Bar Chart. 聽The SGPLOT procedure supports many types of bar charts, each suitable for some specific use case. 聽Today, we will discuss the most common type, the venerable VBAR statement. 聽In this article I will show you many small examples of bar charts with increasing information.

barchartfreqLet us start with the most basic case, as shown on the right. 聽This graph shows the frequency or counts by category with default settings. 聽Click on the graph for a higher resolution image. 聽The SGPLOT code needed to create is very simple, as shown below.

title 'Counts by Type'; proc sgplot data=sashelp.cars; 聽 vbar type; run;

The graph above is rendered to the LISTING destination with default style and default setting for the axes.

barchartrespThe graph on the right shows the mean of city mileage by type. 聽The title already mentions "Mileage by Type", so there is no need to repeat that information as the label of the x-axis. 聽The label is suppressed by the x-axis option.

title 'Mileage by Type'; proc sgplot data=sashelp.cars; 聽 vbar type / response=mpg_city stat=mean 聽 聽 聽 聽 聽 聽barwidth=0.6 fillattrs=graphdata2; 聽 xaxis display=(nolabel); run;

Note, we have specified RESPONSE=mpg_city, with STAT=MEAN. 聽This has to be set as the default STAT is SUM, and there is no point in viewing the sum of the mileage of all cars of one type. 聽Also, we have set BARWIDTH=0.6 and set the bar attributes to GRAPHDATA2 for a change of pace.

barchartresperrorNext, we create a bar chart of mean mileage by type, with display of the 95% confidence limits. 聽A legend is automatically created by the procedure to display the two items in the graph. 聽Also note, I have used GRAPHDATA4 for the bar attributes, and removed the display of the baseline to clean up the display.

title 'Mileage by Type'; proc sgplot data=sashelp.cars; 聽 vbar type / response=mpg_city stat=mean 聽 聽 聽 聽 聽 聽 barwidth=0.6 聽 聽 聽 聽 聽 聽 fillattrs=graphdata4 limits=both 聽 聽 聽 聽 聽 聽 baselineattrs=(thickness=0); 聽 xaxis display=(nolabel); run;

barchartresplabelThe graph on the right shows the mean mileage by type, using options to create a different look and feel. 聽We have also displayed the response value for each bar at the top. 聽A decorative skin is used to make the bars aesthetically pleasing using DATASKIN=matte.

In this graph I have聽suppressed the border around the data area. 聽The axis lines and ticks are removed and y-axis grids are added. 聽This results in a clean graph as shown on the right. 聽Click on the graph for a higher resolution image.

title 'Mileage by Type'; proc sgplot data=sashelp.cars noborder; 聽 format mpg_city 4.1; 聽 vbar type / response=mpg_city stat=mean 聽 聽 聽 聽 聽 聽datalabel dataskin=matte 聽 聽 聽 聽 聽 聽baselineattrs=(thickness=0) 聽 聽 聽 聽 聽 聽fillattrs=(color=&softgreen); 聽 xaxis display=(nolabel noline noticks); 聽 yaxis display=(noline noticks) grid; run;

barchartstackNow, let us add a group classifier using the GROUP=variable option. 聽The SGPLOT procedure summarizes the response data by category and group. 聽Values for each group are stacked for each category, creating a stacked bar chart as shown on the right.

title 'Sales by Type and Quarter for 1994'; proc sgplot data=sashelp.prdsale(where=(year=1994)) noborder; 聽 format actual dollar8.0; 聽 vbar product / response=actual stat=sum 聽 聽 聽 聽 聽 聽group=quarter seglabel datalabel 聽 聽 聽 聽 聽 baselineattrs=(thickness=0) 聽 聽 聽 聽 聽 outlineattrs=(color=cx3f3f3f); 聽 xaxis display=(nolabel noline noticks); 聽 yaxis display=(noline noticks) grid; run;

A stacked bar chart makes sense with聽STAT=SUM (default). 聽Now the bar height is the sum of all the observations for the category. 聽By default, SGPLOT stacks the segments for each group in a category. 聽Note, with SAS 9.4, the segments can be labeled with the value of each segment, and the bar itself can also be labeled with the total value for each bar. 聽Note, a legend showing the color used for each unique value of the group variable is shown.

barchartclusterAnother聽useful graph is聽shown on the right. 聽Here, we have used GROUPDISPLAY=CLUSTER which places the groups side-by-side within each category. 聽A group legend is displayed by default.

title 'Sales by Type and Year'; proc sgplot data=sashelp.prdsale noborder; 聽 vbar product / response=actual 聽 聽 聽 聽 聽 group=year groupdisplay=cluster 聽 聽 聽 聽 聽dataskin=pressed 聽 聽 聽 聽 聽baselineattrs=(thickness=0); 聽 xaxis display=(nolabel noline noticks); 聽 yaxis display=(noline) grid; run;

barchartclustergradientBar values can be shown for each group in a category, as shown on the right. 聽Note, the values are automatically rotated to a vertical orientation when the values will not fit in the space available.

Note the use of the STYLEATTRS statement to set the fill colors for the two group values to gold and olive. 聽This statement allows to control the attributes for the group values for fill colors, contrast colors, marker symbols and line patterns. 聽Also, note the use of FILLTYPE=Gradient to color the bars in an alpha gradient, from fully saturated at the top, to transparent at the bottom.

title 'Sales by Type and Year'; proc sgplot data=sashelp.prdsale noborder; 聽 styleattrs datacolors=(gold olive); 聽 vbar product / response=actual 聽 聽 聽 聽 聽 聽 聽group=year groupdisplay=cluster 聽 聽 聽 聽 聽 dataskin=pressed baselineattrs=(thickness=0) 聽 聽 聽 聽 聽 filltype=gradient datalabel; 聽 xaxis display=(nolabel noline noticks); 聽 yaxis display=(noline) grid; run;

You may have noted that the VBAR statement supports only one GROUP role, which can then be displayed as STACKED or CLUSTERED. 聽SGPLOT does not support a bar chart that has both a CLUSTER and a STACK group like the SAS/GRAPH GCHART statement. 聽Creating such a graph requires some complex layout of the category axis, and a decision was made to avoid such complex axis layouts as this combination is relatively rare.

barchartclusterstackBut, what聽to do if you do need a stacked + clustered bar chart? 聽The solution is to use the SGPANEL procedure as shown below. 聽The resulting graph is shown on the right. 聽Here we have a bar chart of actual sales by type, year and quarter. 聽The year values are side-by-side and the quarter values are stacked.

The SGPANEL procedure below uses the panel variable of product. 聽So, each "cluster" is really a cell in the panel. 聽Each cell contains a stacked bar chart with category of year and group=quarter. 聽Normally, the cell header is at the top of each cell, with a header border. 聽Here, we have moved the header to the bottom of the graph, and suppressed the cell borders, thus making the graph appear like a stacked+clustered bar chart. 聽Note use of COLAXIS instead of XAXIS and ROWAXIS instead of YAXIS.

title 'Sales by Type, Year and Quarter'; proc sgpanel data=sashelp.prdsale; 聽 styleattrs datacolors=(gold olive &softgreen silver); 聽 panelby product / onepanel rows=1 noborder layout=columnlattice 聽 聽 聽 聽 聽 聽 聽 聽 聽noheaderborder novarname colheaderpos=bottom; 聽 vbar year / response=actual stat=sum group=quarter barwidth=1 聽 聽 聽 聽 聽 聽dataskin=pressed baselineattrs=(thickness=0) filltype=gradient; 聽 colaxis display=(nolabel noline noticks) valueattrs=(size=7); 聽 rowaxis display=(noline nolabel noticks) grid; run;

For all the examples above, the data contains one or more classifier variables with one response variable. 聽This is what is sometimes referred to as a "Tall" structure. 聽But often, the data structure is "Wide", like in an Excel table, with聽multiple response columns by聽category.

barchartoverlayIn such a case, it is possible to create a clustered bar chart without transforming the data, by layering the data for each column as shown on the right. 聽Here, we have layered two bar VBAR statements, one for mpg_city and one for mpg_highway, both for the same category variable. 聽Normally, the second layers would cover the first, but we have made the 2nd layer bars narrower, so we can see both.

title 'Mileage by Type'; proc sgplot data=sashelp.cars noborder; 聽 styleattrs datacolors=(olive gold); 聽 vbar type / response=mpg_city stat=mean 聽 聽 聽 聽 聽 聽dataskin=pressed baselineattrs=(thickness=0) ; 聽 vbar type / response=mpg_highway stat=mean 聽 聽 聽 聽 聽 dataskin=pressed baselineattrs=(thickness=0) 聽 聽 聽 聽 聽barwidth=0.5; 聽 xaxis display=(nolabel noline noticks); 聽 yaxis display=(noline) grid; run;

barchartoverlayoffset_2Finally, the bars need not be overlayed on category centers, but can be "offset" to be side-by-side, or even a bit overlapped as shown on the right. 聽Here the bar widths are 0.6, and each VBAR is offset to left or right by 0.1, creating overlapping bars.

title 'Mileage by Type'; proc sgplot data=sashelp.cars noborder; 聽 styleattrs datacolors=(brown olive); 聽 vbar type / response=mpg_highway stat=mean 聽 聽 聽 聽 聽 聽dataskin=pressed barwidth=0.6聽 聽 聽 聽 聽 聽 聽baselineattrs=(thickness=0) 聽 聽 聽 聽 聽 聽discreteoffset=-0.1; 聽 vbar type / response=mpg_city stat=mean 聽 聽 聽 聽 聽 dataskin=pressed barwidth=0.6聽 聽 聽 聽 聽 聽 baselineattrs=(thickness=0) 聽 聽 聽 聽 聽 discreteoffset= 0.1; 聽 xaxis display=(nolabel noline noticks); 聽 yaxis display=(noline) grid; run;

There is one restrictioin when layering multiple VBAR statements. 聽The category variables for all VBAR statements must be the same. 聽If a group is specified, it must be specified for all the VBAR statements in the same way. 聽If this is not the case, the program will stop with an error message in the log. 聽There are other ways to handle such cases that will be discussed later.

These examples聽give you an idea of the versatility of the SGPLOT VBAR statement. 聽You can create bar charts from the simplest to complex and with different aesthetic appearance. 聽I would encourage you to see other examples in this blog on creating bar charts with SGPLOT procedure.

Full code: 聽getting_started_2_vbar

Tags Feature du jour Getting Started with SGPLOT SGPANEL SGPLOT VBAR


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3